package com.bmadmin.controller.cus;

import com.bmadmin.domain.DataQueryItemPOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.service.DataQueryItemService;
import com.bmadmin.util.BMUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 金玮良 on 2017-12-20.
 * sql 语句
 */
@RestController
@RequestMapping("/dataQueryItems")
public class DataQueryItemController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());


    @Autowired
    DataQueryItemService dataQueryItemService;

    @GetMapping("/getList")
    public List<DataQueryItemPOJO> getQueryItemList(DataQueryItemPOJO dataQueryItemPOJO){

        return dataQueryItemService.getDataQueryItemList(dataQueryItemPOJO);
    }

    @PostMapping(value = "/insert",produces = "application/json")
    public NormalResult insertDataItemList(@RequestBody DataQueryItemPOJO dataQueryItemPOJO) {

        NormalResult result = new NormalResult();

        String qListId=BMUtil.NulltoString(dataQueryItemPOJO.getQ_list_id());
        String qDataSourceId=BMUtil.NulltoString(dataQueryItemPOJO.getQ_datasource_id());
        String qName=BMUtil.NulltoString(dataQueryItemPOJO.getQ_name());
        String qSql=BMUtil.NulltoString(dataQueryItemPOJO.getQ_sql());

        if("".equals(qListId)||"".equals(qDataSourceId)||"".equals(qName)||"".equals(qSql)){
            result.setCode("9999");
            result.setMsg("传入部分参数为空");
            return result;
        }

        result = dataQueryItemService.insertDataQueryItem(dataQueryItemPOJO);
        return result;

    }

    @PostMapping("/delete")
    public NormalResult deleteDataItem(DataQueryItemPOJO dataQueryItemPOJO){
        NormalResult result = new NormalResult();

        if("".equals(BMUtil.NulltoString(dataQueryItemPOJO.getQ_id()))){
            result.setCode("9999");
            result.setMsg("传入部分参数为空");
            return result;
        }

        result = dataQueryItemService.deleteDataQueryItem(dataQueryItemPOJO);
        return result;
    }

    @PostMapping(value = "/update",produces = "application/json")
    public NormalResult updateDataItem(@RequestBody DataQueryItemPOJO dataQueryItemPOJO){
        NormalResult result = new NormalResult();

        String qDataSourceId=BMUtil.NulltoString(dataQueryItemPOJO.getQ_datasource_id());
        String qName=BMUtil.NulltoString(dataQueryItemPOJO.getQ_name());
        String qSql=BMUtil.NulltoString(dataQueryItemPOJO.getQ_sql());

        if("".equals(qDataSourceId)||"".equals(qName)||"".equals(qSql)){
            result.setCode("9999");
            result.setMsg("传入部分参数为空");
            return result;
        }

        result = dataQueryItemService.updateDataQueryItem(dataQueryItemPOJO);
        return result;
    }

}
